﻿//---------------------------------------------------------------------------------
// Microsoft (R)  Windows Azure Platform AppFabric SDK
// Software Development Kit
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//---------------------------------------------------------------------------------

namespace Microsoft.ServiceBus.Samples
{
	using System;
	using System.Configuration;
	using System.ServiceModel;
	using ServiceBus;

	internal class Program
	{
		private static void Main()
		{
			var servicePath = ConfigurationManager.AppSettings["ServicePath"];
			var serviceNamespace = ConfigurationManager.AppSettings["ServiceNamespace"];
			var issuerName = ConfigurationManager.AppSettings["IssuerName"];
			var issuerSecret = ConfigurationManager.AppSettings["IssuerSecret"];

			var serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, servicePath);

			var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior
				{
					CredentialType = TransportClientCredentialType.SharedSecret
				};
			sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
			sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;

			Console.WriteLine("CloudTrace Console Connecting ...");

			// The WCF ABCs
			var address = serviceUri;
			var binding = new NetEventRelayBinding();
			var contract = typeof(ITraceContract);

			var serviceHost = new ServiceHost(typeof(TraceService), address);

			var serviceEndpoint = serviceHost.AddServiceEndpoint(contract, binding, String.Empty);
			serviceEndpoint.Behaviors.Add(sharedSecretServiceBusCredential);

			serviceHost.Open();

			Console.WriteLine("Connected To: {0}", address);
			Console.WriteLine("Hit [Enter] to exit");

			Console.ReadLine();

			serviceHost.Close();
		}
	}
}